import { message } from 'antd';
import { getOfferDetail, getEntryDetail, saveEntry } from '@/services/onboardAndLeave/entryDetail';

export default {
    namespace: 'entryDetail',
    state: {
        detail: {}
    },
    effects: {
        *getOfferDetail({ payload }, { put, call }) {
            const result = yield call(getOfferDetail, payload);
            if (!result || result.code !== 10000) {
                message.error(result.msg);
            } else {
                yield put({
                    type: 'save',
                    payload: {
                        detail: result ? result.data : {}
                    }
                });
            }
        },
        *getEntryDetail({ payload }, { put, call }) {
            const result = yield call(getEntryDetail, payload);
            if (!result || result.code !== 10000) {
                message.error(result.msg);
            } else {
                yield put({
                    type: 'save',
                    payload: {
                        detail: result ? result.data : {}
                    }
                });
            }
        },
        *saveEntry({ payload, callback }, { call }) {
            const result = yield call(saveEntry, payload);
            if (result && result.code === 10000) {
                message.success('保存成功');
                if (callback && typeof callback === 'function') {
                    callback();
                }
            } else {
                message.error(result.msg);
            }
        }
    },
    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload
            };
        }
    }
};
